﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class EmpStatusEN : System.Web.UI.Page
{

    public Persons reqPersons
    {
        get
        {
            if (ViewState["reqPersons"] == null)
                ViewState["reqPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["reqPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["reqPersons"] == null)
                ViewState["reqPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["reqPersons"].ToString()] = value;
        }
    }



    public Persons mobjPersons
    {
        get
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPersons"].ToString()] as Persons;
        }
        set
        {
            if (ViewState["mobjPersons"] == null)
                ViewState["mobjPersons"] = System.Guid.NewGuid().ToString();
            Session[ViewState["mobjPersons"].ToString()] = value;
        }
    }



    public List<Courses> lstGridCourses
    {
        get
        {
            if (ViewState["lstGridCourses"] == null)
            {
                ViewState["lstGridCourses"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstGridCourses"].ToString()] == null)
            {
                Session[ViewState["lstGridCourses"].ToString()] = new List<Courses>();

            }

            return (List<Courses>)Session[ViewState["lstGridCourses"].ToString()];

        }
        set
        {
            if (ViewState["lstGridCourses"] == null)
            {
                ViewState["lstGridCourses"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstGridCourses"].ToString()] = value;
        }  



    }



    protected void Page_Load(object sender, EventArgs e)
    {
        Persons objPersons = GlobalVars.getCurrentPerson();

        mobjPersons = objPersons;

        checkSecurityAndCustomizePage(objPersons);

        checkID();

        bindAllControls();

        if (!Page.IsPostBack)
        {
            bindddlLanguage();
            showLanguage();
        }

        bindGrid();

    }


    /// <summary>
    /// this function will get if the language filter will shown or not
    /// </summary>
    private void showLanguage()
    {
        SystemOptionsDB objDB = new SystemOptionsDB(ConnectionStrings.LMS);
        List<SystemOptions> lst = objDB.Select("", "");

        if (lst.Count < 1)
        {
            dvShowLanguage.Visible = true;
            grdCourses.Columns[3].Visible = true;
        }
        else
        {

            dvShowLanguage.Visible = lst[0].ShowCourseLanguage;
            grdCourses.Columns[3].Visible = lst[0].ShowCourseLanguage;
        }


        if (lst.Count < 2)
        {

            grdCourses.Columns[2].Visible = true;
        }
        else
        {


            grdCourses.Columns[2].Visible = lst[1].ShowCourseLanguage;
        }

    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGrid();
    }

    /// <summary>
    /// this function will bind the drop down list of languages
    /// </summary>
    private void bindddlLanguage()
    {
        LanguagesDB objDB = new LanguagesDB(ConnectionStrings.LMS);
        List<Languages> lst = objDB.Select("", "");
        lst = (from obj in lst orderby obj.LanguageNameEN select obj).ToList<Languages>();

        lst.Insert(0, new Languages { ID = -1, LanguageNameAR = "-- الكل --", LanguageNameEN = "-- All --" });

        ddlLanguage.DataSource = lst;
        ddlLanguage.DataTextField = "LanguageNameEN";
        ddlLanguage.DataValueField = "ID";
        ddlLanguage.DataBind();
        ddlLanguage.SelectedValue = "-1";

    }


    /// <summary>
    /// this function will bind controls with employee values
    /// </summary>
    private void bindAllControls()
    {
        lblReqEmployeeBadge.Text = reqPersons.Badge.ToString();
        lblReqEmployeeName.Text = reqPersons.Name_EN + " " + reqPersons.MiddleName_EN + " " + reqPersons.FamilyName_EN;

    }

    /// <summary>
    /// this function will check for ID in Request and if it's belong to current employee
    /// </summary>
    protected void checkID()
    {
       


        long reqID = 0;

        try { reqID = long.Parse(Request["ID"]); }
        catch { Response.Redirect("LoginEN.aspx"); }

        PersonsDB objPersonsDB = new PersonsDB (ConnectionStrings.LMS);

        List<Persons> lstTemp = objPersonsDB.getPersonsByGenID(reqID);

        if (lstTemp.Count == 0)
            Response.Redirect("LoginEN.aspx");


        reqPersons = lstTemp[0];

        if (!mobjPersons.lstAllEmployees.Contains(reqPersons))
            Response.Redirect("LoginEN.aspx");

        

    }



    /// <summary>
    /// this function will bind the grid with the persons
    /// </summary>
    protected void bindGrid()
    {

        reqPersons.clearchilds();


        lstGridCourses = reqPersons.lstCourses;

        if (ddlLanguage.SelectedValue != "-1")
        {
            long language = long.Parse(ddlLanguage.SelectedValue);
            lstGridCourses = (from obj in lstGridCourses where obj.LanguageID == language select obj).ToList<Courses>();
        }

        grdCourses.DataSource = lstGridCourses;
        grdCourses.DataBind();
    }



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check



        if ((!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Manager_page)) && currentPerson.lstAddEmployees.Count == 0)
            Response.Redirect("LoginEN.aspx");




    }
    
   




    




    


   

   
    
    
    protected void grdCourses_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCourses.DataSource = lstGridCourses;
        grdCourses.PageIndex = e.NewPageIndex;
        grdCourses.DataBind();

    }
    
    
    
    
    
    
 


    


    


    protected void grdCourses_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            long editedID = long.Parse(grdCourses.DataKeys[e.Row.RowIndex]["ID"].ToString());

            CoursesDB objCoursesDB = new CoursesDB (ConnectionStrings.LMS);

            List<Courses> lstTemp = objCoursesDB.getCoursesByID(editedID);
            if (lstTemp.Count > 0)
            {
                Label lblSpendTime = (Label)e.Row.FindControl("lblSpendTime");
                
                Label lblCourseStatus = (Label)e.Row.FindControl("lblStatus");


                if (lstTemp[0].isCaptivate)
                {
                    GlobalVars.CustomizeGrid(lstTemp[0], reqPersons, lblSpendTime, new Panel(), lblCourseStatus,false);
                }
                else
                {

                    GlobalVars.CustomizeGridNotCaptivate(lstTemp[0], reqPersons, lblSpendTime, new Panel(), lblCourseStatus,false);
                   
                }




            }
        
        }
    }
}
